package com.novellotus.iov.carvice.model;

import com.amap.api.maps.AMap;
import com.amap.api.maps.Projection;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.novellotus.iov.carvice.activity.MapCarActivity;
import com.novellotus.iov.carvice.bean.MapCarListBean;
import com.novellotus.iov.carvice.retrofit.BaseArrayEntity;
import com.novellotus.iov.carvice.retrofit.BaseArrayObserver;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.retrofit.RxSchedulers;
import com.novellotus.iov.carvice.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;

/**
 * Created by Rich on 2017/8/18.
 */

public class MapCarModel {

    private MapCarActivity context;
    private List<Marker> markersList = new ArrayList<>();
    private MapCarInterface mapCarInterface;

    public MapCarModel(MapCarActivity context) {
        this.context = context;
    }

    /**
     * 调节地图到正好放置查询范围的所有点 * @param centerLatLng 中心点 * @param range 查询范围（米）
     */
    public void getCar(AMap aMap, CameraPosition cameraPosition) {
        Projection mTarget = aMap.getProjection();
        LatLng left = mTarget.getVisibleRegion().latLngBounds.southwest;
        double left_longitude = left.longitude;
        double left_latitude = left.latitude;
        LatLng right = mTarget.getVisibleRegion().latLngBounds.northeast;
        double right_longitude = right.longitude;
        double right_latitude = right.latitude;
        getMapCarList( cameraPosition);
    }

    /**
     * 获取所有车辆
     */
    public void getMapCarList(final CameraPosition cameraPosition) {
        Observable<BaseArrayEntity<MapCarListBean>> observable = RetrofitFactory.getInstance().getAllVehicleData(SharedPreferencesUtils.getKey(context)[0]);
        observable.compose(RxSchedulers.<BaseArrayEntity<MapCarListBean>>compose())
                .subscribe(new BaseArrayObserver<MapCarListBean>(context) {

                    @Override
                    public void onSuccess(List<MapCarListBean> t) {
                        mapCarInterface.onMapLoaded(t, cameraPosition);
                    }

                    @Override
                    public void onFail(int code, String msg) {

                    }

                    @Override
                    public void onError(String msg) {

                    }
                });
    }

    public interface MapCarInterface {
        void onMapLoaded(List<MapCarListBean> t, CameraPosition cameraPosition);
    }
}
